--[[ =================================================================
    Description:
        BagTips gives you a peek at what is in a particular bag
        without having to open the bag

    Download:
        BagTips - http://redeye.xs4all.nl/bt/

    Contact:
        For questions, bug reports visit the website or send an email
        to the following address: wowaddon@xs4all.nl

    Dependencies:
        None

    Credits:
        A big 'Thank You' to all the people at Blizzard Entertainment
        for making World of Warcraft.
    ================================================================= --]]

-- Send message to the default chat frame
    function BagTips_Message( msg, prefix )
        -- Initialize
        local prefixText = "";

        -- Add application prefix
        if ( prefix ) then
            prefixText = C_GREEN.."BT: "..C_CLOSE;
        end;

        -- Send message to chatframe
        DEFAULT_CHAT_FRAME:AddMessage( prefixText..( msg or "" ) );
    end;

-- Add bag contents to tooltip
    function BagTips_OnEnter( self )
        -- Initialize
        local tooltip = GameTooltip;
        local items = {};
        local bagName = self:GetName();
        local bagID = BT_BAG_IDS[bagName];
        local count = 0;

        -- Execute original code
        BT_Hooks[bagName]( self );

        -- Group items by name
        for slotID = 1, GetContainerNumSlots( bagID ) do
            -- Get itemlink
            local itemLink = GetContainerItemLink( bagID, slotID );

            -- Retrieve item information
            if ( itemLink ) then
                local itemName, _, itemQuality, itemLevel, itemMinLevel, itemType, itemSubType, itemStackCount, itemEquipLoc, itemTexture = GetItemInfo( itemLink );
                local _, itemCount = GetContainerItemInfo( bagID, slotID );

                if ( itemCount > 0 ) then
                    if ( not items[itemName] ) then
                        items[itemName] = {};
                        items[itemName].texture = itemTexture;
                        items[itemName].count = itemCount;
                        items[itemName].quality = itemQuality;
                    else
                        items[itemName].count = items[itemName].count + itemCount;
                    end;
                end;
            end;
        end;

        -- Add items to tooltip
        tooltip:AddLine( "|n"..BT_CONTENTS );
        foreach( items, function( key, value )
            local r, g, b, hex = GetItemQualityColor( value.quality );
            tooltip:AddDoubleLine( "  |T"..value.texture..":0|t "..key, value.count, r, g, b );
            count = count + 1;
        end );
        if ( count == 0 ) then
            tooltip:AddDoubleLine( "  "..BT_EMPTY );
        end;

        -- Add free slot info
        tooltip:AddLine( "|n" );
        tooltip:AddDoubleLine( BT_FREESLOTS, BT_FREE:format( GetContainerNumFreeSlots( bagID ) , GetContainerNumSlots( bagID ) ) );

        -- Forces tooltip to properly resize
        tooltip:Show();
    end;

-- Install all of the hooks and tracked events used by BagTips
    function BagTips_Install_Hooks()
        -- Show startup message
        BagTips_Message( BT_STARTUP_MESSAGE, false );

        -- Save original OnEnter/UpdateTooltip functions
        BT_Hooks["MainMenuBarBackpackButton"] = MainMenuBarBackpackButton:GetScript( "OnEnter" );
        BT_Hooks["CharacterBag0Slot"] = CharacterBag0Slot.UpdateTooltip;
        BT_Hooks["CharacterBag1Slot"] = CharacterBag1Slot.UpdateTooltip;
        BT_Hooks["CharacterBag2Slot"] = CharacterBag2Slot.UpdateTooltip;
        BT_Hooks["CharacterBag3Slot"] = CharacterBag3Slot.UpdateTooltip;
        BT_Hooks["KeyRingButton"] = KeyRingButton:GetScript( "OnEnter" );

        -- Set new OnEnter/UpdateTooltip functions
        MainMenuBarBackpackButton:SetScript( "OnEnter", BagTips_OnEnter );
        CharacterBag0Slot.UpdateTooltip = BagTips_OnEnter;
        CharacterBag1Slot.UpdateTooltip = BagTips_OnEnter;
        CharacterBag2Slot.UpdateTooltip = BagTips_OnEnter;
        CharacterBag3Slot.UpdateTooltip = BagTips_OnEnter;
        KeyRingButton:SetScript( "OnEnter", BagTips_OnEnter );
    end;
